﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Transactions;
using System.Net.Mail;
using System.Configuration;

namespace LeadExpress.Utility
{
    public class UserEmailToken
    {
        /// <summary>
        /// Send email
        /// </summary>
        /// <param name="subjectline"></param>
        /// <param name="fromEmailAddress"></param>
        /// <param name="toEmailAddress"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static bool SendEmail(string subjectline, string fromEmailAddress, string toEmailAddress, string body)
        {
            System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
            System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(fromEmailAddress);

            // Assign email object to information of this contact.
            email.Subject = subjectline;
            email.To.Add(toEmailAddress);
            email.From = from;
            email.IsBodyHtml = true;
            email.Body = body;

            //put this in a transaction
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    SmtpClient smtpClient = new SmtpClient();

                    if (ConfigurationSettings.AppSettings["SmtpHostValue"] != null)
                    {
                        smtpClient.Host = ConfigurationSettings.AppSettings["SmtpHostValue"].ToString();
                    }
                    else
                    {
                        smtpClient.Host = "";
                    }

                    if (ConfigurationSettings.AppSettings["SmtpPortValue"] != null)
                    {
                        smtpClient.Port = int.Parse(ConfigurationSettings.AppSettings["SmtpPortValue"].ToString());
                    }
                    else
                    {
                        smtpClient.Port = 25;
                    }

                    smtpClient.Send(email);

                    //end transaction
                    scope.Complete();

                    //assign status
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Send an email to user
        /// </summary>
        /// <param name="subjectline"></param>
        /// <param name="fromEmailAddress"></param>
        /// <param name="toEmailAddress"></param>
        /// <param name="_object"></param>
        /// <param name="success"></param>
        public static void SendEmail(string subjectline, string fromEmailAddress, string toEmailAddress, User _object, ref bool success)
        {
            System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
            System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(fromEmailAddress);

            string body = string.Empty;
            body = "First Name: " + _object.FirstName;
            body = "Last Name: " + _object.FirstName;
            body += "Email: " + _object.Email;
            body += "Password: " + _object.Password;

            // Assign email object to information of this contact.
            email.Subject = subjectline;
            email.To.Add(toEmailAddress);
            email.From = from;
            email.IsBodyHtml = true;
            email.Body = body;

            //put this in a transaction
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    SmtpClient smtpClient = new SmtpClient();
                    if (ConfigurationSettings.AppSettings["SmtpHostValue"] != null)
                    {
                        smtpClient.Host = ConfigurationSettings.AppSettings["SmtpHostValue"].ToString();
                    }
                    else
                    {
                        smtpClient.Host = "";
                    }

                    if (ConfigurationSettings.AppSettings["SmtpPortValue"] != null)
                    {
                        smtpClient.Port = int.Parse(ConfigurationSettings.AppSettings["SmtpPortValue"].ToString());
                    }
                    else
                    {
                        smtpClient.Port = 25;
                    }

                    smtpClient.Send(email);

                    //end transaction
                    scope.Complete();

                    //assign status
                    success = true;
                }
                catch (Exception)
                {
                    success = false;
                }
            }
        }
    }
}